﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.Drawing.Imaging;
using System.IO;
using System.Timers;
using System.Threading;
using System.Diagnostics;
using Content.Web;

/*
 * IMPORTANT NOTE!
 * 
 * The file ImageService.svc cannot exist within the SolutionFramework.Web project.  If it exists
 * there, it causes VS to crash on build of the Silverlight project. A post build task copies
 * from here to the Web project.
 */

namespace Content.Web.ImageService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ImageService : IImageService
    {
        public void UploadImage(Guid key, string fileName, string table, byte[] imageData)
        {
            var entities = new eLearningEntities();

            switch (table)
            {
                case "BulletPoint":

                    var bulletPoint = entities.BulletPoints.Single(b => b.BulletPointID == key);

                    bulletPoint.ImageFileName = fileName;
                    bulletPoint.ImageData = imageData;

                    break;

                case "Unit":

                    var unit = entities.Units.Single(t => t.UnitID == key);

                    unit.ImageFileName = fileName;
                    unit.ImageData = imageData;

                    break;

                case "ContentRow":

                    var contentRow = entities.ContentRows.Single(c => c.ContentRowID == key);

                    contentRow.ImageFileName = fileName;
                    contentRow.ContentRowImageData = imageData;

                    break;
            }

            entities.SaveChanges();
        }

        public string ExtensionFromFormat(Guid format)
        {
            if (ImageFormat.Jpeg.Guid.Equals(format))
            {
                return "jpg";
            }
            else if (ImageFormat.Gif.Equals(format))
            {
                return "gif";
            }
            else if (ImageFormat.Png.Equals(format))
            {
                return "png";
            }
            else if (ImageFormat.Bmp.Equals(format))
            {
                return "bmp";
            }
            else if (ImageFormat.Tiff.Equals(format))
            {
                return "tif";
            }

            return null;
        }

        public Guid? FormatFromFileName(string fileName)
        {
            var ext = Path.GetExtension(fileName);

            if (ext.IndexOf('.') >= 0)
                ext = ext.Substring(ext.LastIndexOf('.') + 1);
            switch (ext.ToLower())
            {
                case "jpg": return (Guid?)ImageFormat.Jpeg.Guid;
                case "gif": return (Guid?)ImageFormat.Gif.Guid;
                case "png": return (Guid?)ImageFormat.Png.Guid;
                case "bmp": return (Guid?)ImageFormat.Bmp.Guid;
                case "tif": return (Guid?)ImageFormat.Tiff.Guid;
            }
            return null;
        }
    }
}
